import Vue from 'vue'
import VueRouter from 'vue-router'

// 使用Vue Router插件
Vue.use(VueRouter)

// 定义路由数组
const routes = [
    {path: '/', name: 'HomeView', component: () => import('@/views/HomeView.vue'),
        redirect: '/home', // 重定向主页
        children: [
            {path: 'person', name: 'person', meta: {name: '个人信息'}, component: () =>
                    import('@/views/Person.vue'),},
            {path: 'password', name: 'password', meta: {name: '修改密码'}, component: () =>
                    import('@/views/Password.vue'),},
            {path: 'home', name: 'home', meta: {name: '系统首页'}, component: () =>
                    import('@/views/Home.vue'),},

            {path: 'course_reserve', name: 'course_reserve', meta: {name: '课程预约'}, component: () =>
                    import('@/views/Student/Course_reserve.vue'),},
            {path: 'car_reserve', name: 'car_reserve', meta: {name: '车辆预约'}, component: () =>
                    import('@/views/Student/Car_reserve.vue'),},
            {path: 'course_record', name: 'course_record', meta: {name: '课程预约记录'}, component: () =>
                    import('@/views/Student/Course_record.vue'),},
            {path: 'car_record', name: 'car_record', meta: {name: '车辆预约记录'}, component: () =>
                    import('@/views/Student/Car_record.vue'),},
            {path: 'comment', name: 'comment', meta: {name: '用户评价'}, component: () =>
                    import('@/views/Student/Comment.vue'),},
            {path: 'message', name: 'message', meta: {name: '消息通知'}, component: () =>
                    import('@/views/Student/Message.vue'),},

            {path: 'chart', name: 'chart', meta: {name: '统计图表'}, component: () =>
                    import('@/views/ManagerAndCoach/Chart.vue'),},
            {path: 'course_info', name: 'course_info', meta: {name: '课程信息'}, component: () =>
                    import('@/views/ManagerAndCoach/Course_info.vue'),},
            {path: 'course_check', name: 'course_check', meta: {name: '课程预约审核'}, component: () =>
                    import('@/views/ManagerAndCoach/Course_check.vue'),},
            {path: 'car_info', name: 'car_info', meta: {name: '车辆信息'}, component: () =>
                    import('@/views/ManagerAndCoach/Car_info.vue'),},
            {path: 'car_check', name: 'car_check', meta: {name: '车辆预约审核'}, component: () =>
                    import('@/views/ManagerAndCoach/Car_check.vue'),},
            {path: 'notice', name: 'notice', meta: {name: '公告信息'}, component: () =>
                    import('@/views/ManagerAndCoach/Notice.vue'),},
            {path: 'comment_info', name: 'comment_info', meta: {name: '评价信息'}, component: () =>
                    import('@/views/ManagerAndCoach/Comment_info.vue'),},

            {path: 'manager_info', name: 'manager_info', meta: {name: '管理员信息'}, component: () =>
                    import('@/views/Manager/Manager_info.vue'),},
            {path: 'coach_info', name: 'coach_info', meta: {name: '教练信息'}, component: () =>
                    import('@/views/Manager/Coach_info.vue'),},
            {path: 'student_info', name: 'student_info', meta: {name: '学员信息'}, component: () =>
                    import('@/views/Manager/Student_info.vue'),},
        ]
    },

    {path: '/login', name: 'login', meta: {name: '登录'}, component: () =>
            import('@/views/Login.vue'),},
    {path: '/register', name: 'register', meta: {name: '注册'}, component: () =>
            import('@/views/Register.vue'),},
    {path: '/403', name: '403', meta: {name: '无权访问'}, component: () =>
            import('@/views/403.vue'),},
    {path: '*', name: '404', meta: {name: '无法访问'}, component: () =>
            import('@/views/404.vue'),},
]

// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题。
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
    return originalPush.call(this, location).catch(err => err)
}

// 创建Vue Router实例
const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// 全局路由导航守卫
router.beforeEach((to, from, next) => {
    let user = JSON.parse(localStorage.getItem('honey-user') || '{}')

    let managerPath = ['/manager_info', '/coach_info', '/student_info']
    if (user.role !== '管理员' && managerPath.includes(to.path)) {
        next('/403')
    } else {
        next()
    }

    let managerAndCoachPath = ['/course_info', '/course_check', '/car_info', '/car_check', '/notice', '/comment_info', '/notice']
    if ((user.role !== '管理员' && user.role !== '教练' && managerAndCoachPath.includes(to.path))) {
        next('/403')
    } else {
        next()
    }

    let studentPath = ['/course_reserve', '/car_reserve', '/course_record', '/car_record', '/comment', '/message']
    if ((user.role !== '学员' && studentPath.includes(to.path))) {
        next('/403')
    } else {
        next()
    }

    let userPath = ['/person', '/password']
    if ((!user.role && userPath.includes(to.path))) {
        next('/403')
    } else {
        next()
    }
})

// 导出Vue Router实例，供其他模块使用
export default router

